
class Dashboard::AvatarsController < Dashboard::BaseController
  before_action :require_logined

  def update
    if params[:image]
      avatar = Avatar.new
      avatar.image = params[:image]
      if avatar.valid?
        current_avatar = current_user.avatar
        avatar.user = current_user
        if current_avatar
          if current_avatar.try(:destroy!) && current_avatar.destroyed? && avatar.save
            render_success("上传头像成功", image_url: avatar.image_url)
          else
            render_fail("上传头像失败")
          end
        else
          if avatar.save
            render_success("上传头像成功", image_url: avatar.image_url)
          else
            render_fail("上传头像失败")
          end
        end
      else
        render_fail("您上传的头像格式不支持")
      end
    else
      render_fail("请选择头像后上传")
    end
  end
end
